/* 
 * Copyright (C) 2007  Brunata a/s
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 */

package com.brunata.httprmi.sample;

import com.brunata.httprmi.*;

/**
 * Classic hello world sample using the HttpRmi API
 * 
 * A dynamic proxy factory is used to create an instance of an interface at 
 * runtime. The SampleClient happens to implement ProgressListener, such that
 * we can pass it as the 3'rd argument to DynamicProxyFactory.create(...) and
 * have receive progress messages.
 * An ongoing call can also be cancelled, by casting the dynamic proxy instance
 * to <B>ICancellable</B> and invoking the cancel() method. Java does not support
 * preemptive thread cancelling, so the call might still block during I/O until 
 * the next 4Kb is fetched from the internal buffer. The cancel() method will 
 * eventually cause an unchecked CancelledException to be thrown to the SampleClient.
 * 
 * Note: To conform to the given contract, no checked exceptions are ever used. 
 * In other words, realize that while you are not obliged to, you really should
 * catch CancelledException, InvalidEndpointException and HttpRmiException
 * 
 * @author Casper Bang (cab@brunata.com)
 */
public class SampleClient implements IStakeHolder
{
    public SampleClient()
    {
        SampleContract contract = DynamicProxyFactory.create(SampleContract.class, "http://localhost:8080/SampleServer/SampleServlet", this);
        
        try
        {
            System.out.println( contract.getHello() );
        }
        catch(InvalidEndpointException ex)
        {
            System.out.println("Invalid endpoint, port or server!");
        }
        catch(HttpRmiException ex)
        {
            System.out.println("Internal endpoint error!");
        }
    }
 
    @Override
    public void progressChangeEvent(int level, int min, int max)
    {
        System.out.println( level +" bytes out of " + max + " received...");
    }

    @Override
    public boolean isCancelled()
    {
        // We do not allow cancellation in this small sample. If you wish to provide this,
        // set a cancellation flag and have this method reflect its value. The proxy will
        // consult this callback periodically (every 4Kb read from the remnote gateway)
        return false;
    }
    
    public static void main(String... args) 
    {
        new SampleClient();
    }    
}
